/*
 * ConfigurationFrame.java
 *
 * Created on December 11, 2003, 10:19 AM
 */

package gov.nist.applet.phone.ua.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.dtec.common.util.SettingUtil;
import com.dtec.resource.settingInfo.Account;

import gov.nist.applet.phone.ua.*;

/**
 * 
 * @author DERUELLE Jean
 */
public class ConfigurationFrame extends javax.swing.JFrame {
	MessengerManager sipMeetingManager;
	Configuration configuration;
	String path = "xmlSource/ServerInfo.xml";

	private Account accountEdit = null;
	private SysConfigFrame scfHandler = null;

	final static int OPERATION_ADD = 0;
	final static int OPERATION_EDIT = 1;

	/** Creates new form ConfigurationFrame */
	public ConfigurationFrame(MessengerManager sipMeetingManager) {
		setConfigurationFrame(sipMeetingManager);
	}

	public ConfigurationFrame(MessengerManager sipMeetingManager,
			SysConfigFrame handler) {
		setConfigurationFrame(sipMeetingManager);
		this.scfHandler = handler;
	}

	private void setConfigurationFrame(MessengerManager sipMeetingManager) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception exe) {
			exe.printStackTrace();
		}
		this.sipMeetingManager = sipMeetingManager;
		configuration = sipMeetingManager.getMessageListener()
				.getConfiguration();
		initComponents();
		jtfUsername.setText(configuration.name + "@"
				+ configuration.outboundProxy);
		jtfPort.setText(String.valueOf(configuration.proxyPort));
		jpfPassword.setText(configuration.password);

	}

	public ConfigurationFrame(Account account, SysConfigFrame handler) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception exe) {
			exe.printStackTrace();
		}
		initComponents();
		if (this.sipMeetingManager == null) {
			this.setTitle("修改账户");
			jlTitle.setText("修改账户");
			jbOK.setText("修改");
		}
		this.accountEdit = account;
		this.scfHandler = handler;
		jtfUsername
				.setText(account.getName() + "@" + account.getProxyaddress());
		jtfPort.setText(String.valueOf(account.getProxyport()));
		jpfPassword.setText(account.getPassword());
	}

	public ConfigurationFrame() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception exe) {
			exe.printStackTrace();
		}
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	private static final long serialVersionUID = 1L;

	private JPanel contentPane = null;
	private JLabel jlTitle = null;
	private JLabel jlUsername = null;
	private JTextField jtfUsername = null;
	private JLabel jlPassword = null;
	private JPasswordField jpfPassword = null;
	private JLabel jlPort = null;
	private JTextField jtfPort = null;
	private JCheckBox jcbKeep = null;

	private JButton jbAdvance = null;
	private JButton jbOK = null;
	private JButton jbCancel = null;

	private JPanel getContentPanel() {
		contentPane = new JPanel() {
			private static final long serialVersionUID = 1L;

			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				ImageIcon img = new ImageIcon(
						"images/addAccount/newAccount.png");
				g.drawImage(img.getImage(), 0, 0, null);
			};
		};
		contentPane.setBackground(Color.WHITE);
		contentPane.setLayout(null);

		jlTitle = new JLabel("新建账户", new ImageIcon(
				"images/addAccount/account.png"), 0);
		jlTitle.setSize(200, 40);
		jlTitle.setLocation(100, 5);
		jlTitle.setFont(new Font("宋体", Font.PLAIN, 18));
		contentPane.add(jlTitle);

		jlUsername = new JLabel("用户名");
		jlUsername.setSize(80, 30);
		jlUsername.setLocation(70, 50);
		jlUsername.setFont(new Font("宋体", Font.PLAIN, 15));

		jtfUsername = new JTextField();
		jtfUsername.setSize(160, 30);
		jtfUsername.setLocation(130, 50);
		jtfUsername.setOpaque(false);
		contentPane.add(jlUsername);
		contentPane.add(jtfUsername);

		jlPassword = new JLabel("密  码");
		jlPassword.setSize(80, 30);
		jlPassword.setLocation(70, 90);
		jlPassword.setFont(new Font("宋体", Font.PLAIN, 15));

		jpfPassword = new JPasswordField();
		jpfPassword.setSize(160, 30);
		jpfPassword.setLocation(130, 90);
		jpfPassword.setOpaque(false);
		contentPane.add(jlPassword);
		contentPane.add(jpfPassword);

		jlPort = new JLabel("端  口");
		jlPort.setSize(80, 30);
		jlPort.setLocation(70, 130);
		jlPort.setFont(new Font("宋体", Font.PLAIN, 15));

		jtfPort = new JTextField();
		jtfPort.setSize(60, 30);
		jtfPort.setLocation(130, 130);
		jtfPort.setOpaque(false);
		contentPane.add(jlPort);
		contentPane.add(jtfPort);

		jcbKeep = new JCheckBox("设置为默认账户");
		jcbKeep.setSelected(true);
		jcbKeep.setSize(115, 30);
		jcbKeep.setLocation(130, 170);
		jcbKeep.setOpaque(false);
		contentPane.add(jcbKeep);

		jbAdvance = new JButton("高级");
		jbAdvance.setSize(60, 30);
		jbAdvance.setLocation(40, 210);
		jbAdvance.setFont(new Font("宋体", Font.PLAIN, 12));
		jbAdvance.setHorizontalTextPosition(JButton.CENTER);
		jbAdvance.setOpaque(false);

		contentPane.add(jbAdvance);

		jbOK = new JButton("添加");
		jbOK.setSize(60, 30);
		jbOK.setLocation(230, 210);
		jbOK.setFont(new Font("宋体", Font.PLAIN, 12));
		jbOK.setHorizontalTextPosition(JButton.CENTER);
		jbOK.setOpaque(false);

		contentPane.add(jbOK);

		jbCancel = new JButton("取消");
		jbCancel.setSize(60, 30);
		jbCancel.setLocation(300, 210);
		jbCancel.setFont(new Font("宋体", Font.PLAIN, 12));
		jbCancel.setHorizontalTextPosition(JButton.CENTER);
		jbCancel.setOpaque(false);

		contentPane.add(jbCancel);

		return contentPane;
	}

	private void initComponents() {

		this.setSize(400, 300);
		this.setTitle("添加新帐号");
		this.setLayout(null);
		this.setContentPane(getContentPanel());
		this.setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);// 显示在屏幕中央
		setVisible(true);

		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				exitForm(evt);
			}
		});

		jbOK.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jbOKActionPerformed(evt);
			}
		});

		jbCancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jbCancelActionPerformed(evt);
			}
		});

	}

	private void jbCancelActionPerformed(java.awt.event.ActionEvent evt) {
		this.dispose();
	}

	private void jbOKActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			int indexAT = jtfUsername.getText().indexOf("@");
			if (indexAT < 0) {
				jtfUsername.setForeground(Color.RED);
				return;
			}
			String name = jtfUsername.getText().substring(0, indexAT);

			String password = String.copyValueOf(jpfPassword.getPassword());
			if (password.equals("")) {
				jpfPassword.setForeground(Color.RED);
				return;
			}

			String proxyaddress = jtfUsername.getText().substring(indexAT + 1,
					jtfUsername.getText().length());
			if (proxyaddress.equals("")) {
				jtfUsername.setForeground(Color.RED);
				return;
			}

			String proxyport = jtfPort.getText();
			Pattern p = Pattern.compile("^[1-9]\\d*$");
			Matcher m = p.matcher(proxyport);
			if (!m.matches()) {
				jtfPort.setForeground(Color.RED);
				return;
			}
			String protocal = "udp";
			if (!("".equals(name) || "".equals(password)
					|| "".equals(proxyaddress) || "".equals(proxyport) || ""
					.equals(protocal))) {
				Account account = this.accountEdit == null ? new Account()
						: this.accountEdit;
				if (this.accountEdit == null) {
					account.setId(UUID.randomUUID().toString());
				}
				account.setName(name);
				account.setPassword(password);
				account.setProxyaddress(proxyaddress);
				account.setProxyport(proxyport);
				account.setProtocal(protocal);
				account.setDefaulAccount(jcbKeep.isSelected() ? "1" : "0");
				if (this.sipMeetingManager == null) {
					SettingUtil.editAccout(account, path);
					scfHandler.freshAccountsList();
				} else {
					SettingUtil.addAccount(account, path);
					if(scfHandler!=null)scfHandler.freshAccountsList();
					sipMeetingManager.setUserURI(configuration.userURI);
					configuration.outboundProxy = jtfUsername.getText()
							.substring(indexAT + 1,
									jtfUsername.getText().length());
					try {
						configuration.proxyPort = Integer.parseInt(jtfPort
								.getText());
					} catch (NumberFormatException nfe) {
						nfe.printStackTrace();
					}
					configuration.mediaTransport = "udp";
					sipMeetingManager.getMessageListener().resetOutBoundProxy();
				}
				this.dispose();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void exitForm(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_exitForm
		this.dispose();
	}

}
